package com.yangyang.hw3hbase.service;

import com.yangyang.hw3hbase.utils.HBaseUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service
public class HBaseService {

    @Resource
    HBaseUtils hBaseUtils;

    /**
     * 判断表是否存在
     * @param tableName
     * @return
     */
    public boolean exist(String tableName) {
        return hBaseUtils.isExists(tableName);
    }

    /**
     * 创建表
     * @param tableName
     * @param fm
     */
    public void createTable(String tableName, String fm) {
        hBaseUtils.createTable(tableName, Collections.singletonList(fm));
    }

    /**
     * put
     */
    public void putData() {

//        hBaseUtils.putData("yangyang", "student1", "name", "", "value1-1");

        hBaseUtils.putData("yangyang", "row1", "student",
                Arrays.asList("name", "info:student_id", "info:class", "score:understanding", "score:programming"),
                Arrays.asList("Tom", "20210000000001", "1", "75", "82"));
        hBaseUtils.putData("yangyang", "row2", "student",
                Arrays.asList("name", "info:student_id", "info:class", "score:understanding", "score:programming"),
                Arrays.asList("Jerry", "20210000000001", "1", "75", "82"));
        hBaseUtils.putData("yangyang", "row3", "student",
                Arrays.asList("name", "info:student_id", "info:class", "score:understanding", "score:programming"),
                Arrays.asList("Jack", "20210000000001", "1", "75", "82"));
        hBaseUtils.putData("yangyang", "row4", "student",
                Arrays.asList("name", "info:student_id", "info:class", "score:understanding", "score:programming"),
                Arrays.asList("Rose", "20210000000001", "1", "75", "82"));
        hBaseUtils.putData("yangyang", "row5", "student",
                Arrays.asList("name", "info:student_id", "info:class"),
                Arrays.asList("yangyang", "G20200343040239", "5"));
    }

    /**
     * 获取表所有数据
     * @param tableName
     * @return
     */
    public List<Map<String, String>> getAll(String tableName) {
        return hBaseUtils.getData(tableName);
    }

    /**
     * 删除行
     * @param tableName
     * @param rows
     */
    public void deleteRow(String tableName, String... rows) {
        try {
            hBaseUtils.deleteData(tableName, Arrays.asList(rows));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 卸载表
     * @param tableName
     */
    public void deleteTable(String tableName) {
        try {
            hBaseUtils.dropTable(tableName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
